﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfQuickBrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            string[] weekDays = { "www.naver.com", "www.daum.net", "www.google.com" };
            foreach(string weekStr in weekDays) {
                comboBox1.Items.Add(weekStr);
            }
        }
        private void go()
        {
            string inputStr =  comboBox1.Text;
            bool isUrl = false;
            if (inputStr.StartsWith("http://")
                || inputStr.StartsWith("https://"))
            {
                isUrl = true;
            }
            int periodIndex = inputStr.LastIndexOf('.');
            if (periodIndex == inputStr.Length - 3
                || periodIndex == inputStr.Length - 4)
            {
                isUrl = true;
            }
            if (!isUrl)
            {
                string googleStr = "http://www.google.co.kr/search?q=";
                // 삼성 두산 한국 시리즈

                string[] words = inputStr.Split(new Char[] { ' ' });

                comboBox1.Text = googleStr + String.Join("+", words);

                //comboBox1.Text = googleStr + inputStr.Replace(' ','+');
            }
            //webBrowser1.Navigate(comboBox1.Text, false);
            Navigate(comboBox1.Text);

        }
        private Uri getUri(string urlStr)
        {
            if (!urlStr.ToUpper().StartsWith("HTTP"))
            {
                urlStr = "http://" + urlStr;
            }
            return new Uri(urlStr, UriKind.RelativeOrAbsolute);

        }
        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox1.SelectedItem == null)
            {
                return;
            }
            string urlStr = comboBox1.SelectedItem.ToString();

            Navigate(urlStr);
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                go();
            }
        }

        private void Navigate(string urlStr)
        {
            if (urlStr.Trim().Equals(""))
            {
                return;
            }

            webBrowser1.Navigate(getUri(urlStr));

        }
        private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
        {

        }
    }
}
